﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MyOA.Model;

namespace MyOA.DAL
{
   public class OperateFileService
    {
       DBHelper db = new DBHelper();
       /// <summary>
       /// 根据文件ID找这文件的PARENTID
       /// </summary>
       /// <param name="fileId">文件ID</param>
       /// <returns>返回parentid</returns>
       public int getParentIdByFileId(int fileId)
       {
           return (int)db.getScalar("sp_getParentIdByFileId", new SqlParameter("fileId", fileId));
       }
       /// <summary>
       /// 根据文件ID来修改文件信息
       /// </summary>
       /// <param name="fileInfo">文件ID</param>
       /// <returns>是否修改成功</returns>
       public bool updateFileInfoByFileId(FileInfo fileInfo)
       {
           SqlParameter[] values = new SqlParameter[]
            {
                new SqlParameter("@fileId",fileInfo.FileId),
                new SqlParameter("@fileTypeId",fileInfo.FileType.FileTypeId),
                new SqlParameter("@filesName",fileInfo.FilesName),
                new SqlParameter("@remark",fileInfo.Remark)
            };
           if (db.getCount("sp_updateFileInfoByFileId_Flie", values) > 0)
           {
               return true;
           }
           else
           {
               return false;
           }
       }

       /// <summary>
       /// 根据文件ID 查找文件信息
       /// </summary>
       /// <param name="fileId">文件ID</param>
       /// <returns>文件信息</returns>
       public FileInfo getFileInfoByFileId(int fileId)
       {
           FileInfo fileInfo = new FileInfo();
           using (SqlDataReader reader = db.getDataReader("sp_getFileInfoByFileId", new SqlParameter("@fileId", fileId)))
           {
               if (reader.Read())
               {
                   fileInfo.FilesName = (string)reader[0];
                   fileInfo.FilePath = (string)reader[1];
                   fileInfo.Remark = (string)reader[2];
                   fileInfo.CreateDate = (DateTime)reader[3];
                   fileInfo.FileOwner.LoginId = (string)reader[4];
                   fileInfo.FileType.FileTypeId = (int)reader[5];
               }
           }
           db.CloseConnection();
           return fileInfo;
       }
       /// <summary>
       /// 根据登陆名查找登陆ID
       /// </summary>
       /// <param name="loginId">登陆名</param>
       /// <returns>登陆ID</returns>
       public int getUserIdByLoginId(string loginId)
       {
           return (int)db.getScalar("sp_getUserIdByLoginId", new SqlParameter("@loginId", loginId));
       }

       /// <summary>
       /// 获取全部的文件类型信息
       /// </summary>
       /// <returns>全部的文件类型信息</returns>
       public IList<FileTypeInfo> getAllTypeInfo()
       {
           IList<FileTypeInfo> fileTypeInfos = new List<FileTypeInfo>();
           using (SqlDataReader reader=db.getDataReader("sp_getAllFileTypeInfo"))
           {
               while (reader.Read())
               {
                   FileTypeInfo fileTypeInfo = new FileTypeInfo();
                   fileTypeInfo.FileTypeId = (int)reader[0];
                   fileTypeInfo.FileTypeImage = (string)reader[2];
                   fileTypeInfos.Add(fileTypeInfo);
               }
           }
           db.CloseConnection();
           return fileTypeInfos;
       }

       /// <summary>
       /// 新增文件信息
       /// </summary>
       /// <param name="fileInfo">要新增的文件信息</param>
       /// <returns>是否新增成功</returns>
       public bool insertFileInfo(FileInfo fileInfo)
       {
           SqlParameter[] values = new SqlParameter[]
            {
                new SqlParameter("@filesName",fileInfo.FilesName),
                new SqlParameter("@filePath",fileInfo.FilePath),
                new SqlParameter("@remark",fileInfo.Remark),
                new SqlParameter("@createDate",fileInfo.CreateDate),
                new SqlParameter("@parentId",fileInfo.ParentId),
                new SqlParameter("@fileOwnerId",fileInfo.FileOwner.UserId),
                new SqlParameter("@fileTypeId",fileInfo.FileType.FileTypeId)
            };
           if (db.getCount("sp_insertFileInfo_Flie", values) > 0)
           {
               return true;
           }
           else
           {
               return false;
           }
       }
      
    }
}
